using UnityEngine;
using System.Collections;

public class MoveTouch : MonoBehaviour
{


	private RaycastHit hit;
	private int layerMask = 1 << 8;
	Vector3 offset;
	// Use this for initialization
	void Start ()
	{
		
	}

	// Update is called once per frame
	void Update ()
	{
		
		foreach (Touch touch in Input.touches) {
			
			if(touch.fingerId == 0) {
			
				if (touch.phase == TouchPhase.Began) {
					
					Ray ray = Camera.main.ScreenPointToRay (touch.position);
					
					if (Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask)) {
			
						offset = hit.point - transform.position;
					}
				}
			
				if (touch.phase == TouchPhase.Moved) {
					Ray ray = Camera.main.ScreenPointToRay (touch.position);
					
					if (Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask)) {
						transform.position = hit.point - offset ;
					}
				}
			}
		}
		
	}
	
	
}
